	Ext.define('Module.task.MultiVideoPlay', {
	extend : 'Ext.window.Window',
	layout: 'fit',
	border: false,
	width: 400,height: 300,
	requires: ['Sh.ux.VideoPlayer'],
	maximized: true,
	closeAction: 'destroy',
	config: {
		rwdata: {},
		rwtype: 0,
		roles: [],//角色对象,[{"jsname":"32324","jsid":44,"bzid":82},{"jsname":"23","jsid":47,"bzid":82}]
		gcs: [],
		play: true,
		alltime: 0,
		index: -1
	},
	initComponent : function() {
		var me = this;
		me.play = true;
		me.title = '任务：'+me.rwdata.content;
		me.items = [{
			xtype: 'form',
			layout: 'column',
			bodyPadding: 3,
			autoScroll: true,
			defaults: {columnWidth: 1,xtype: 'fieldset'},
			items: [{
				layout: 'column',
				tbar: ['多角度视频回放'],
				id: me.id + '-roles',
//				height: 435,
				xtype: 'panel',
				defaults: {
					columnWidth: 1/3,
					name: 'role-panel',
					padding: 5,
					layout: 'fit',
					border: true,
					height: 350,
					html: '<div style="background: black;color:white;width:100%;height:100%;"><br>&nbsp;&nbsp;没有视频。</div>'
				}/*,
				listeners: {
					added: function(_this,c,index){
						
					}
				}*/
			},{
				layout: 'column',
				defaults: {columnWidth: 1,padding: '0 0 10 0'},
				height: 100,
				items: [{
					xtype: 'panel',
					layout: 'column',
					border: false,
					id: me.id + '-gcs',
					height: 30,
					defaults: {padding: '0 3 0 3',frame: true},
					items: []
				},{
					xtype:'progressbar',
					id: me.id+'-progress',
					width:150,
					style: {cursor: 'pointer','font-size': '12px'},
					listeners:{
						afterrender: function(prbar){
							var runner = new Ext.util.TaskRunner();
							me.task = runner.newTask({
								run: function () {
									try {
										var items = Ext.ComponentQuery.query('player',me);
										var maxtime = 0;
										for ( var i = 0; i < items.length; i++) {
											var t = items[i].getCurrentTime();
											var rp = items[i].ownerCt;
											if(rp.videos.length > 1 && rp.vindex > 0){
												for ( var vi = 0; vi < rp.vindex; vi++) {
													t += Number(rp.videos[vi].videotime/1000);
												}
											}
											
											me.changeVideo(rp);
											
											if(t){
												maxtime = maxtime > t ? maxtime : t;
											}
										}
										
										var bfgctime = 0;
										for ( var i = 0; i < me.index; i++) {
											bfgctime += me.gcs[i].mt;
										}
										
										var alltime = maxtime + bfgctime;
										
										Ext.getCmp(me.id+'-play-alltime').update({
											html: '<span style="font-size:12px;">'+timeFormat(alltime)+'</span>'
										});
										
										if(maxtime >= me.gcs[me.index].mt-1){
											var index = me.index;
											var gcs = me.gcs;
											if(index + 1 < gcs.length){
												me.switchGc(me.index + 1);
											}
										}
										
										prbar.updateProgress((alltime/me.alltime),'');
										
									} catch (e) {}
								},
								interval: 1000
							}).start();

							//视频进度点击事件
							prbar.el.on('click',function(e,t){
								var el = prbar.el; 
								var x = e.getX();
								var _t = (x-el.getLeft())*(me.alltime/prbar.getWidth());
								var gt = 0;
								var ptime = _t;
								for ( var i = 0; i < me.gcs.length; i++) {
									gt += me.gcs[i].mt;
									if(_t < gt){
										me.switchGc(i,ptime);
										break;
									}
									
									ptime = ptime - me.gcs[i].mt;
								}
							});
						}
					}

				},{
					xtype:'panel',
					border: false,
		            layout: {
		                type: 'hbox',
		                align: 'middle'
		            },
		            defaults: {margin: 5},
		            items: [{xtype: 'label',text: '00:00',id: me.id+'-play-alltime'},
		                    {xtype: 'tbfill'},{
		            	xtype: 'button',
		            	iconCls: 'icon-v_pause',
		            	handler: function(){
		            		if(me.play){
		            			var items = Ext.ComponentQuery.query('player',me);
		            			for ( var i = 0; i < items.length; i++) {
		            				items[i].pause();
								}
		            			me.play = false;
		            		}
		            	}
		            },{
		            	xtype: 'button',
		            	iconCls: 'icon-v_play',
		            	handler: function(){
		            		if(!me.play){
		            			var items = Ext.ComponentQuery.query('player',me);
		            			for ( var i = 0; i < items.length; i++) {
		            				items[i].play();
								}
		            			me.play = true;
		            		}
		            		
		            	}
		            },{
		            	xtype: 'button',
		            	iconCls: 'icon-v_stop',
		            	handler: function(){
		            		if(me.play){
		            			var items = Ext.ComponentQuery.query('player',me);
		            			for ( var i = 0; i < items.length; i++) {
		            				items[i].pause();
								}
		            			me.play = false;
		            		}
		            	}
		            },{xtype: 'tbfill'},{id: me.id+'-alltime',xtype: 'label',text: '00:00'}]
				}]
			},{
				title: '任务详细内容',
				layout: 'fit',
				height: 260,
				items: [{
					xtype: 'form',
					border: false,
					defaults: {anchor: '100%',readOnly:true},
					items: [{
						xtype: 'textarea',
						fieldLabel: '任务内容'
					},{
						xtype: 'textfield',
						fieldLabel: '班组'
					},{
						xtype: 'textfield',
						fieldLabel: '工作地点'
					},{
						xtype: 'textfield',
						fieldLabel: me.rwtype == 2?'是否完成':'完成阶段'
					},{
						xtype: 'textfield',
						fieldLabel: '计划开始时间'
					},{
						xtype: 'textfield',
						fieldLabel: '计划结束时间'
					}]
				}]
			}]
		}];
		
		me.on('afterrender',function(){
			Ext.Ajax.request({
				async: true,
				method: 'POST',
				params: {taskId: me.rwdata.id,taskType:me.rwtype,bzid: me.rwdata.bz_id},
				url:  'djd/getJs.do', 
				success: function(response, options) {
					var data = Ext.decode(response.responseText);
					if(data && data.roles){
						me.setRoles(data.roles);
						me.setGcs(data.gcs);
						var rp = Ext.getCmp(me.id + '-roles');
						Ext.getCmp(me.id + '-roles').setHeight((Math.ceil(me.roles.length/3))*350+35);
						for ( var i = 0; i < me.roles.length; i++) {
							var role = me.roles[i];
							if(role.vsize > 0){
								rp.add({
									xtype: 'panel',
									tbar: ['角色：'+role.jsname],
									role: role,
									defaults: {
										listeners: {
											boxready: function(p){
												p.down('toolbar').setVisible(false);
											}
										}
									}
								});
							}
						}
						
						
						var gcsp = Ext.getCmp(me.id + '-gcs');
						gcsp.removeAll();
						for ( var i = 0; i < me.gcs.length; i++) {
							var gc = me.gcs[i];
							me.alltime += gc.mt;
						}
						
						Ext.getCmp(me.id+'-alltime').update({
							html: '<span style="font-size:12px;">'+timeFormat(me.alltime)+'</span>'
						});
						
						for ( var i = 0; i < me.gcs.length; i++) {
							var gc = me.gcs[i];
							gcsp.add({
								html: '<center>'+gc.zygc_name+'</center>',
								columnWidth: gc.mt/me.alltime,
								height: 26
							});
						}
					}
					me.switchGc(0,0);
				},
				failure: function(response, options){
					
				}
			});
			
			var taskInfo = me.down('form').items.items[2].down('form').items.items;
			taskInfo[0].setValue(me.rwdata.content);
			taskInfo[1].setValue(me.rwdata.bz_name);
			taskInfo[2].setValue(me.rwtype == 2?me.rwdata.transformer:me.rwdata.bdz_name);
			taskInfo[3].setValue(me.rwtype == 2?(me.rwdata.isfinish == 0?'否':'是'):me.rwdata.f_stage);
			taskInfo[4].setValue(formatDate(me.rwdata.starttime));
			taskInfo[5].setValue(formatDate(me.rwdata.endtime));
		});
		
		
		me.callParent();
	},
	changeVideo: function(rolep){
		var player = rolep.down('player');
		if(rolep.videos.length > 1 && rolep.vindex < rolep.videos.length-1){
			if(player.getCurrentTime() + 0.5 >= player.getAllTime()){
				rolep.vindex += 1;
				var video = rolep.videos[rolep.vindex];
				rolep.removeAll();
				rolep.add({
					xtype: 'player',
					autoPlay: true,
					vdata: {id: video.id},
					src: video.synpath,
					oldtime: 0,
					hides: ['play','progress','time','full','sign']
				});
			}
		}
		
	},
	switchGc: function(index,time){
		time = time || 0;
		var me = this;
		var gcid = me.gcs[index].id;
		if(me.index != index){
			me.index = index;
			var rp = Ext.getCmp(me.id + '-roles');
			var ritems = rp.items.items;
			for ( var i = 0; i < ritems.length; i++) {
				var rolep = ritems[i];
				rolep.removeAll();
				var videos = rolep.role.videos[gcid];
				rolep.videos = videos;
				rolep.vindex = 0;
				var t = 0;
				for ( var vi = 0; vi < videos.length; vi++) {
					var video = videos[vi];
					if(t >= time || (vi == videos.length - 1)){
						rolep.vindex = vi;
						rolep.add({
							xtype: 'player',
							autoPlay: true,
							vdata: {id: video.id},
							src: video.synpath,
							oldtime: time - t,
							hides: ['play','progress','time','full','sign']
						});
						break;
					}
					
					t += (video.videotime/1000);
				}
			}
		}
		else{
			var rp = Ext.getCmp(me.id + '-roles');
			var ritems = rp.items.items;
			for ( var i = 0; i < ritems.length; i++) {
				var rolep = ritems[i];
				var videos = rolep.videos;
				var t = 0,bt=0;
				for ( var vi = 0; vi < videos.length; vi++) {
					var video = videos[vi];
					var _t = (video.videotime/1000);
					t += _t;
					if(time < t || (vi == videos.length - 1)){
						if(rolep.vindex == vi){
							var p = rolep.down('player');
							if(p)p.goToTime(time - bt);
						}
						else{
							rolep.removeAll();
							rolep.vindex = vi;
							rolep.add({
								xtype: 'player',
								autoPlay: true,
								vdata: {id: video.id},
								src: video.synpath,
								oldtime: time - bt,
								hides: ['play','progress','time','full','sign']
							});
						}
						
						break;
					}
					
					bt += _t;
				}
			}
		}
	}
});